package com.hbnu.iostream;

import java.io.*;

/**
 * 需求：使用字符输入流读取指定文件中的内容，并输出到控制台中
 *
 * @author kittychen
 * @since 2025-10-11 下午 2:57
 */
public class ReaderDemo {

    public static void main(String[] args) {
        String path = "E:" + File.separator + "2310" + File.separator + "writer.txt";
        File file = new File(path);

        Reader reader = null;
        try {
            reader = new FileReader(file);

            char[] buf = new char[1024];

            // int len = reader.read(buf);

            // 自己完成一个字符一个字符的读
            int len = 0;
            int temp = 0;
            while ((temp = reader.read()) != -1) {
                buf[len++] = (char) temp;
            }

            System.out.println(new String(buf, 0, len));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
